package blueBridgeCup;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class demo7 {

    static int sx, sy, ex, ey, m, n;
    static boolean[][] visited;

    static int[]dx={0,0,1,-1};
    static int[]dy={1,-1,0,0};
    static Queue<int[]> queue = new LinkedList<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        m = in.nextInt();
        n = in.nextInt();
        int[][] G = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                G[i][j] = in.nextInt();
            }
        }
        sx = in.nextInt() - 1;
        sy = in.nextInt() - 1;
        ex = in.nextInt() - 1;
        ey = in.nextInt() - 1;

        in.close();
        visited = new boolean[m][n];

        System.out.println(bfs(sx, sy,G));

    }

    static int bfs(int i,int j,int[][]G){
queue.offer(new int[]{i,j});
int ret=0;
visited[i][j]=true;
while (!queue.isEmpty()){
    int size= queue.size();
    ret++;
    for (int k = 0; k <size ; k++) {
        int[]poll= queue.poll();
       int a=poll[0];
       int b=poll[1];
        if(a==ex&&b==ey)return ret-1;
        for(int f=0;f<4;f++){
         int x=a+dx[f];
         int y=b+dy[f];
         if(x>=0&&x<m&&y>=0&&y<n&&!visited[x][y]&&G[x][y]==1){
             visited[x][y]=true;
          queue.offer(new int[]{x,y});
         }
        }
    }
}
return -1;
    }
}